/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */
 
/**
  ******************************************************************************
  * @file    ch2601_lvgl
  * @version V1.0
  * @date    2022-04-04
  * @author  码农爱学习(xxpcb)
  * @brief   RVB2601 lvgl测试
  * @attention 本例程基于平头哥RVB2601的ch2601_gui_demo例程进行修改
  * 实验平台:平头哥RVB2601
  * 个人博客:https://xxpcb.gitee.io
  ******************************************************************************
  */


/*********************
 *      INCLUDES
 *********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <aos/aos.h>
#include "aos/cli.h"

#include "app_init.h"
#include "lvgl.h"
#include "lv_label.h"
#include "lv_checkbox.h"
#include "oled.h"

/*********************
 *      DEFINES
 *********************/
#define TAG "app"

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
// GUI
static void gui_lvgl_task(void *arg);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

volatile uint32_t g_debug   = 0;
volatile uint32_t g_debug_v = 0;

//static void gui_lvgl_task(void *arg);
static void gui_lvgl_task2(void *arg);
static void get_time_task(void *arg);

#include "csi_core.h"
/**
 * main
 */
int main(void)
{
    board_yoc_init();

    //aos_task_new("gui", gui_lvgl_task, NULL, 10 * 1024);
	
	aos_task_new("gui", gui_lvgl_task2, NULL, 10 * 1024);
	//aos_task_new("rtc", get_time_task, NULL, 10 * 1024);
	
	
    return 0;
}

/*************************** Task1 *****************************/
#if 0
static void gui_label_create(void)
{
    lv_obj_t *p = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(p, LV_LABEL_LONG_BREAK);
    lv_label_set_align(p, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_pos(p, 0, 4);
    lv_obj_set_size(p, 128, 60);
    lv_label_set_text(p, "THEAD RISC-V\nGUI\nDEMO");
}

static void gui_lvgl_task(void *arg)
{
    lv_init();
    /*Initialize for LittlevGL*/
    oled_init();

    /*Select display 1*/
    // demo_create();
    gui_label_create();

    while (1) {
        /* Periodically call the lv_task handler.
         * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();

        aos_msleep(5);
        lv_tick_inc(1);
    }
}
#endif

/*************************** Task2 *****************************/
#if 0
static void gui_label_create2(void)
{
    lv_obj_t *label1 = lv_label_create(lv_scr_act(), NULL); //创建lable对象 labe1
    lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);    /*显示模式固定*/ 
	lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);      /*居中显示*/
	lv_label_set_text(label1,"RISC-V\nRVB2601");            /*显示标签的内容*/
	lv_obj_set_pos(label1, 0, 0);                           /*显示的起始坐标*/                  
	lv_obj_set_size(label1, 128, 30);                       /*显示框的大小*/
    lv_obj_set_width(label1, 128);                          /*设置显示的宽度*/
    lv_obj_align(label1, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);  /*靠项端居中显示*/
	
    lv_obj_t * label2 = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label2, LV_LABEL_LONG_SROLL_CIRC);  /*轮动显示*/
	lv_label_set_text(label2, "Hello EEWOLRD，2022-03-11！");
    lv_obj_set_width(label2, 150);
    lv_obj_align(label2, NULL, LV_ALIGN_CENTER, 0, 20);   
}
#else

static void obj_test(void)
{
	lv_obj_t * obj1;
    obj1 = lv_obj_create(lv_scr_act(), NULL);
    lv_obj_set_size(obj1, 200, 100);
    //lv_obj_set_style(obj1, &lv_style_plain_color);
    lv_obj_align(obj1, NULL, LV_ALIGN_IN_LEFT_MID, 0, 0);

    lv_obj_t * obj2;
    obj2 = lv_obj_create(lv_scr_act(), obj1);
    //lv_obj_set_style(obj2, &lv_style_pretty_color);
    lv_obj_align(obj2, NULL, LV_ALIGN_CENTER, 0, -100);
    
    lv_obj_t * obj3;
    obj3 = lv_obj_create(lv_scr_act(), obj2);
    //lv_obj_set_style(obj3, &style_shadow);
    lv_obj_set_pos(obj3,400,300);
}

static void gui_label_create2(void)
{
#if 0
	/*Create an Arc*/
	lv_obj_t * arc = lv_arc_create(lv_scr_act(), NULL);
	lv_obj_set_size(arc, 150, 150);
	lv_arc_set_rotation(arc, 135);
	lv_arc_set_bg_angles(arc, 0, 270);
	lv_arc_set_value(arc, 40);
	lv_obj_align(arc, NULL, LV_ALIGN_CENTER, 0, 0);
#else
	static lv_point_t line_points[] = { {5, 5}, {70, 20}, {120, 10} };

	static lv_style_t style_line;
	lv_style_init(&style_line);
	lv_style_set_line_width(&style_line, LV_STATE_DEFAULT, 2);
	lv_style_set_line_rounded(&style_line, LV_STATE_DEFAULT, true);
	
	lv_obj_t * line1 = lv_line_create(lv_scr_act(), NULL);
	lv_line_set_points(line1, line_points, 3);    
	lv_obj_add_style(line1, LV_LINE_PART_MAIN, &style_line);  
	//lv_obj_align(line1, NULL, LV_ALIGN_CENTER, 0, 0);
	
	obj_test();
#endif                  
}
#endif

static void gui_lvgl_task2(void *arg)
{
    lv_init();
    oled_init();

    gui_label_create2();

    while (1) {
        /* Periodically call the lv_task handler.
         * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();

        aos_msleep(5);
        lv_tick_inc(1);
    }
}
